<template>
  <div class="search">
    <van-nav-bar
      fixed
      title="商品列表"
      left-arrow
      @click-left="$router.go(-1)"
    />

    <van-search
      readonly
      shape="round"
      background="#ffffff"
      show-action
      :value="querySearch || '搜索商品'"
      @click="$router.push('/search')"
    >
      <template #action>
        <van-icon class="tool" name="apps-o" />
      </template>
    </van-search>

    <!-- 排序选项按钮 -->
    <div class="sort-btns">
      <div class="sort-item" @click="sortQuery('all')">综合</div>
      <div class="sort-item" @click="sortQuery('sales')">销量</div>
      <div :class="{active:isActive,'sort-item':true}" @click="sortQuery('price')">价格 <van-icon :name="sortPriceState ? 'arrow-up':'arrow-down'" /></div>
    </div>

    <div class="goods-list">
      <GoodsItem
        v-for="item in proList"
        :key="item.goods_id"
        :item="item"
      ></GoodsItem>
    </div>
  </div>
</template>

<script>
import GoodsItem from "@/components/GoodsItem.vue";
import { getProduct } from "@/api/product";
export default {
  name: "searchList",
  data() {
    return {
      page: 1, // 页数
      proList: [], // 商品列表
      sortParams: "all", // 默认综合排序
      sortParamsPrice: "0", //默认价格升序
      sortPriceState:true, // 排序标记
      isActive:false
    };
  },
  created() {
    this.getProList();
  },
  methods: {
    // 查询数据
    async getProList() {
      const {
        data: { list },
      } = await getProduct({
        categoryId: this.$route.query.categoryId,
        sortType: this.sortParams,
        sortPrice: this.sortParamsPrice,
        goodsName: this.querySearch,
        page: this.page,
      });
      this.proList = list.data;
      console.log(list);
    },

    // 选择排序方式
    sortQuery(query) {
      this.sortParams = query;

      if (this.sortParams === "price") {
      this.sortPriceState = !this.sortPriceState
        this.sortPriceState
          ? (this.sortParamsPrice = "0")
          : (this.sortParamsPrice = "1");
          this.isActive = true
      }else{
        this.sortPriceState = true
        this.isActive = false
      }

      this.getProList();
    },
  },
  computed: {
    querySearch() {
      // 注意是$route，不是$router
      return this.$route.query.search;
    },
  },
  components: {
    GoodsItem,
  },
};
</script>

<style lang="less" scoped>
.search {
  padding-top: 46px;
  ::v-deep .van-icon-arrow-left {
    color: #333;
  }
  .tool {
    font-size: 24px;
    height: 40px;
    line-height: 40px;
  }

  .sort-btns {
    display: flex;
    height: 36px;
    line-height: 36px;
    .sort-item {
      text-align: center;
      flex: 1;
      font-size: 16px;
    }
  }
}

// 商品样式
.goods-list {
  background-color: #f6f6f6;
}

.active{
  color: #ffcb6b;
}

</style>